package coms.process;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import com.crc.mam.BaseCase;
import coms.AutoException;
import coms.Constants;
import frame.FrameWork;
import frame.InterTest;
import functions.ems_claim.InitPaymentPreparation;
import functions.ems_payment.PayBatchController;
import functions.ems_payment.Payment;

/**
 * 
 * @author CHENHAILIAN6
 * 付款工作台-- 发起付款准备-- 创建付款准备
 */
public class PrepaymentRequest extends BaseCase implements InterTest{

	@Override
	public void run(Map<Object, Object> paramMap) {
		// TODO Auto-generated method stub
	  String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }

	  LoginTest loginTest = new LoginTest();
	  loginTest.run(paramMap);
	  if(!loginTest.status) {
		   remark = loginTest.result;
		   status = false;
		   throw new AutoException("登录失败，阻碍测试");
	  }
		/**
		 * 进入付款工作台--发起付款准备--
		 */
		InitPaymentPreparation pre = new InitPaymentPreparation();	
		pre.findInitPaymentPreparationByCondition(null);
		JSONArray array = pre.getInitPaymentPreparations();
		int size = array.length();
		/**
		 * 随机抽一条数据
		 */
		Random dom = new Random(size);
		int temp = dom.nextInt(size -1);
		JSONObject object = array.getJSONObject(temp);
	//	System.out.println(object.toString());
		int id = (int) object.get("payRequestLineId") ;
	//	System.out.println(id);
		int[] payRequestIds = new int[1];
		payRequestIds[0] = id;
		
		paramMap.put("payRequestIds", payRequestIds);
		/**
		 * 创建付款准备
		 */
		Payment payment = new Payment();
		payment.create(paramMap);
		
		JSONObject resultJsonObject = payment.getResultJsonObject();
		String payBatchId = resultJsonObject.getString("payBatchId");
		String batchNo = resultJsonObject.getString("batchNo");
		PayBatchController payBatch = new PayBatchController();
		paramMap.put("batchNo", batchNo);
		payBatch.queryPayBatchInfo(paramMap);
		paramMap.put("batchId", payBatchId);
		payment.getPayLineDetailsByGroup(paramMap);
	   

	}

  @Override
  public boolean getStatus() {
    // TODO Auto-generated method stub
    return status;
  }

  @Override
  public String getRemark() {
    // TODO Auto-generated method stub
    return remark;
  }
	
}
